﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.款式管理.service;
using DXApplicationTangche.UC.款式选择;
using DXApplicationTangche.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.款式管理
{
    public partial class Frm款式维护记录新建 : DevExpress.XtraEditors.XtraForm
    {
        private 款式维护记录 dto款式维护记录 = new 款式维护记录();
        private List<StyleCategoryDTO> styleCategoryDTOs = new List<StyleCategoryDTO>();
        private List<DTOstyleCategory> dTOstyleCategories = new List<DTOstyleCategory>();
        private String styleName;
        public Frm款式维护记录新建()
        {
            InitializeComponent();
            this.styleCategoryDTOs = FunctionHelper.JsonDeserialization<List<StyleCategoryDTO>>(RestCall.httpGetMethod(URL.接口url + "/style/getStyleCategoryAll"));
            this.searchLookUpEdit款式类别.Properties.DataSource = this.styleCategoryDTOs;
        }
        public Frm款式维护记录新建(String styleId)
        {
            InitializeComponent();
            this.styleCategoryDTOs = FunctionHelper.JsonDeserialization<List<StyleCategoryDTO>>(RestCall.httpGetMethod(URL.接口url + "/style/getStyleCategoryAll"));
            this.searchLookUpEdit款式类别.Properties.DataSource = this.styleCategoryDTOs;
            this.textBox款式id.Text = styleId;
        }

        #region 选择款式类别
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn款式类别_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit款式类别.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {               
            }
            
        }
        private void searchLookUpEdit1View_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            this.dto款式维护记录.styleCategoryId = FunctionHelper.GetgridViewValue(this.searchLookUpEdit1View, e.RowHandle, "id");
            this.dto款式维护记录.styleCategoryName = FunctionHelper.GetgridViewValue(this.searchLookUpEdit1View, e.RowHandle, "name");
        }
        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn款式类别_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit款式类别.ToolTip = null;
            searchLookUpEdit款式类别.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.dto款式维护记录.styleCategoryName;
        }
        #endregion

        private void textBox款式id_TextChanged(object sender, EventArgs e)
        {
            this.dto款式维护记录.styleId = this.textBox款式id.Text.Trim();
        }

        private void textBox款号_TextChanged(object sender, EventArgs e)
        {
            this.dto款式维护记录.styleCode = this.textBox款号.Text.Trim();
        }

        private void textBox系列_TextChanged(object sender, EventArgs e)
        {
            this.dto款式维护记录.serials = this.textBox系列.Text.Trim();
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            //this.dto款式维护记录.id = new FunctionHelper().Uuid;
            //this.dto款式维护记录.fitImport = false;
            //this.dto款式维护记录.styleImport = false;
            //RestCall.PostHttp(URL.接口url + "/style/saveStyleIndex", FunctionHelper.JsonSerialization(this.dto款式维护记录));
            this.getStyleName();
            this.dTOstyleCategories.Add(new DTOstyleCategory(this.dto款式维护记录.styleId, this.dto款式维护记录.styleCode, this.styleName));
            RestCall.PostHttp(URL.接口url + "/style/saveStyleCategoryRelation?categoryId=" + this.dto款式维护记录.styleCategoryId, FunctionHelper.JsonSerialization(this.dTOstyleCategories));
            this.Close();
        }
        /// <summary>
        /// 取款式名称
        /// </summary>
        private void getStyleName()
        {
            try
            {
                StyleInfoDto styleInfoDto = 款式Service.getStyleInfoByStyleId(Convert.ToInt32(this.dto款式维护记录.styleId));
                String[] Name = styleInfoDto.styleNameCn.Split(' ');
                //String sql = "SELECT\n" +
                //        "	STYLE_NAME_CN \n" +
                //        "FROM\n" +
                //        "	s_style_p \n" +
                //        "WHERE\n" +
                //        "	SYS_STYLE_ID = '" + this.dto款式维护记录.styleId + "'";
                //String[] Name = SQLmtm.GetDataRow(sql)["STYLE_NAME_CN"].ToString().Split(' ');
                try
                {
                    this.styleName = Name[1];
                }
                catch
                {
                    this.styleName = Name[0];
                }
            }
            catch { }
        }
    }
}
